//File: RegisterUserView.java

package view;

import java.util.logging.Level;
import java.util.logging.Logger;
import ucm.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import model.QueryModel;
import model.User;

/**
 * Creates a view to register a user.
 * 
 * @author Tobias Robinson
 */  
public class RegisterUserView extends JDialog implements ActionListener {
    
    //input to pass to UCRegisterUser    
    private String username = null;
    private String password = null;
    private String userType = null;
    
    //components of the login window    
    private JTextField usernameField = new JTextField(10);
    private JPasswordField passwordField = new JPasswordField(10);	   
    private JLabel usernameLabel = new JLabel("Enter username:  ");
    private JLabel passwordLabel = new JLabel("Enter password:  ");
    private JButton loginButton = new JButton("Register");

    /**
     * Creates a view to register user.
     * 
     * @param parent tells view to run over main window
     */      
    public RegisterUserView(JFrame parent, String type) {
        super(parent, "Register User", true);
        setResizable(false);        
        passwordField.setEchoChar('*');

        //set usertype
        userType = type;
        
        //content pane for login window        
        JPanel loginPane = new JPanel();
        setContentPane(loginPane);
        
        //user GridBag layout        
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        loginPane.setLayout(gb);
        loginPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        // place the username label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 10, 5, 0);
        gb.setConstraints(usernameLabel, c);
        loginPane.add(usernameLabel);

        // place the text field for the username
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        gb.setConstraints(usernameField, c);
        loginPane.add(usernameField);

        // place password label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(0, 10, 10, 0);
        gb.setConstraints(passwordLabel, c);
        loginPane.add(passwordLabel);

        // place the password field
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(0, 0, 10, 10);
        gb.setConstraints(passwordField, c);
        loginPane.add(passwordField);

        // place the login button
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 10, 5, 10);
        c.anchor = GridBagConstraints.CENTER;
        gb.setConstraints(loginButton, c);
        loginPane.add(loginButton);

        // end of layout

        // Register password field and OK button with action event handler.
        // An action event is generated when the return key is pressed while
        // the cursor is in the password field or when the OK button is pressed.
        passwordField.addActionListener(this);
        loginButton.addActionListener(this);
       
        //place the cursor in the username field        
        usernameField.requestFocus();
    }

    /**
     * When user click button or presses enter processes information.
     * 
     * @param e event type
     */
    public void actionPerformed(ActionEvent e) {
            username = new String(usernameField.getText());
            password = new String(passwordField.getPassword());
            User add = new User();

            try {
                add.checkUser(username);
            } catch (SQLException ex) {
                Logger.getLogger(RegisterUserView.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            //if username or password is empty notify user to check input
            //else notify user about account creation            
            if (username == null || password == null) {
                //if username or password is empty notify user to check input                
            }
            else
            {
                //notify user                
                if (add.notifyUser()) {
                    JOptionPane.showMessageDialog(null, "Sorry " + username + ", already in database", "Error", JOptionPane.WARNING_MESSAGE);
                    usernameField.setText("");
                    passwordField.setText("");
                    usernameField.requestFocus();
                }
                else {
                    add.registerUser(username, password, userType);
                    JOptionPane.showMessageDialog(null, "Account " + username + " created", "Account created", JOptionPane.WARNING_MESSAGE);
                    dispose();
                } 
            }   
    }
}